package com.hvisions.repository;

import com.hvisions.entity.Permission;

import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @Author: Lau
 * @Description:
 * @Date: Created in 15:41 2019/11/5
 * @Modified By:
 */
public interface PermissionRepository extends JpaRepository<Permission,Integer>,JpaSpecificationExecutor<Permission>{

    List<Permission> findAllByStatusOrderByOrderNum(Integer status);

    @Query(nativeQuery = true,value = "SELECT DISTINCT p.id ,p.permission_id,p.name,p.description,p.url,p.perms,p.parent_id,p.type,p.order_num,p.icon,p.`status`,p.create_time,p.update_time " +
            "        from permission  p LEFT JOIN role_permission rp" +
            "        on p.permission_id = rp.permission_id" +
            "        LEFT JOIN user_role ur" +
            "        on rp.role_id = ur.role_id" +
            "        WHERE ur.user_id = ?1" +
            "        and p.type in(0,1) and p.status=1" +
            "        ORDER BY p.order_num")
    List<Permission> selectMenuByUserId(String userId);


    @Query(nativeQuery = true,value = "SELECT DISTINCT p.perms\n" +
            "        from permission  p LEFT JOIN role_permission rp\n" +
            "        on p.permission_id = rp.permission_id\n" +
            "        LEFT JOIN user_role ur\n" +
            "        on rp.role_id = ur.role_id\n" +
            "        WHERE ur.user_id = ?1\n" +
            "        and p.perms is not null and p.perms != '' and p.status =1")
    Set<String> findPermsByUserId(String userId);

    @Query(nativeQuery = true,value = "SELECT  b.* FROM role_permission a LEFT JOIN permission b on a.permission_id = b.permission_id WHERE a.role_id = ?1 and b.status=1")
    List<Permission> findByRoleId(String roleId);


    //List<Permission> selectAllMenuName(Integer status);

    @Query(nativeQuery = true,value = "SELECT  count(b.id) FROM permission a  LEFT JOIN permission b on a.id = b.parent_id and b.status=1 WHERE a.permission_id = ?1")
    int selectSubPermsByPermissionId(String permissionId);

    @Modifying
    @Query(nativeQuery = true,value = "UPDATE\n" +
            "        permission\n" +
            "        SET\n" +
            "        status=?2, update_time=now()\n" +
            "        where\n" +
            "        permission_id = ?1")
    @Transactional
    int updateStatusByPermissionId(String permissionId,Integer status);


    Permission findByPermissionId(String permissionId);

    @Query(value = "UPDATE\n" +
            "        Permission p\n" +
            "        SET\n" +
            "        p.description=:#{#per.description},p.url=:#{#per.url},p.perms=:#{#per.perms}, p.parentId=:#{#per.parentId}, p.orderNum=:#{#per.orderNum}, p.icon=:#{#per.icon}\n" +
            "        where\n" +
            "        p.permissionId = :#{#per.permissionId}")
    @Transactional
    @Modifying
    int updateByPermissionId(@Param("per") Permission per);






}
